<?php
namespace app\home\model;

//加载配置类
use think\Config;
//导入Controller
use think\Model;
//导入Db
use think\Db;
//引入Session类
use think\Session;

class LoginModel extends Model
{
	/**
	*	获取验证码
	*/
	public function getCode($phone)
	{	
		Session::clear();
		$code = rand(100000,999999);
		Session::set('code',$code);
		Session::set('u_tel',$phone);
		$SmsDemo = new \Org\aliyun\api_demo\SmsDemo();
		$data = $SmsDemo->sendSms($phone,$code);
		return ['phone'=>$phone,'data'=>$data];
	}

	/**
	*	验证登录信息
	*/
	public function postDologin($data)
	{
		if($data){
			$res = Db::table('sz_user')->where('u_tel',$data['u_tel'])->find();
			if($res['recom_id']!==null){
				Session::set('user_id',$res['id']);
				return 1;
			}else{
				$u_tel = Session::get('u_tel');
				$code = Session::get('code');

				if($res !== null){
					/* 该手机号码已注册 */
					$msg = '该手机号码已注册';
					return ['error'=>2];

				}elseif($data['u_tel'] !==$u_tel){

					$msg = '手机号码与发短信的号码不一致';
					return ['error'=>3,'msg'=>$msg];

				}elseif((int)$data['code']!==$code){

					$msg = '验证码错误';
					return ['error'=>4,'msg'=>$msg];

				}else{
					/* 手机号码未注册 */
					return 5;
				}
			}
		}
	}

	/**
	*	注册会员
	*/
	public function addHomeUser($data)
	{
		if($data){
			
			// 将发布时间转为时间戳
	        $data['registr_time']    = strtotime(date('Y-m-d H:i',time()));

	        // 默认会员头像
	        $data['u_icon'] = '/static/img/headicon.png';

			$res = Db::table('sz_user')->insertGetId($data);
			Session::set('user_id',$res);
			return true;
		}else{
			return false;
		}
	}

}